package com.go.where.family.jersey.api;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.glassfish.jersey.client.JerseyWebTarget;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.go.where.family.jersey.common.EasemobConstants;
import com.go.where.family.jersey.common.HTTPMethod;
import com.go.where.family.jersey.common.Roles;
import com.go.where.family.jersey.utils.JerseyUtils;
import com.go.where.family.jersey.vo.ClientSecretCredential;
import com.go.where.family.jersey.vo.Credential;
import com.go.where.family.jersey.vo.EndPoints;

/**
 * REST API Demo : 用户体系集成 Jersey2.9实现
 * 
 * Doc URL: http://www.easemob.com/docs/rest/userapi/
 * 
 * @author Lynch 2014-09-09
 * 
 */
public class EasemobIMUsers {

	private static final Logger logger = LoggerFactory
			.getLogger(EasemobIMUsers.class);
	private static final String APPKEY = EasemobConstants.APPKEY;
	private static final JsonNodeFactory factory = new JsonNodeFactory(false);

	// 通过app的client_id和client_secret来获取app管理员token
	private static Credential credential = new ClientSecretCredential(
			EasemobConstants.APP_CLIENT_ID, EasemobConstants.APP_CLIENT_SECRET,
			Roles.USER_ROLE_APPADMIN);

	public static void main(String[] args) {
		/**
		 * 注册IM用户[单个]
		 */
		// ObjectNode datanode = JsonNodeFactory.instance.objectNode();
		// datanode.put("username", "kenshinnuser100");
		// datanode.put("password", EasemobConstants.DEFAULT_PASSWORD);
		// ObjectNode createNewIMUserSingleNode =
		// createNewIMUserSingle(datanode);
		// if (null != createNewIMUserSingleNode) {
		// logger.info(createNewIMUserSingleNode.get("statusCode").toString());
		// createNewIMUserSingleNode.get("statusCode").asInt();
		// logger.info("注册IM用户[单个]: " + createNewIMUserSingleNode.toString());
		// }
		// String userName = "test123";
		// String password = userName;
		// ObjectNode createNewIMUserSingleNode = registerIMUser(userName,
		// password);
		// if (null != createNewIMUserSingleNode) {
		// logger.info("注册IM用户[单个]: " + createNewIMUserSingleNode.toString());
		// }

		// String[] usernames = { "kenshinnuser100", "dflob6yi" };
		// String ownerName = "wg9uotow";
		// ObjectNode addblacklistNode = addBlacklist(ownerName, usernames);
		// if (addblacklistNode != null) {
		// logger.info("IM用户加入黑名单: " + addblacklistNode.toString());
		// }

		// String blockedName = "dflob6yi";
		// ObjectNode deleteblacklistNode = deleteBlacklist(ownerName,
		// blockedName);
		// if (deleteblacklistNode != null) {
		// logger.info("IM用户加入黑名单: " + deleteblacklistNode.toString());
		// }

		/**
		 * IM用户登录
		 */
		// ObjectNode imUserLoginNode = imUserLogin(datanode.get("username")
		// .asText(), datanode.get("password").asText());
		// if (null != imUserLoginNode) {
		// LOGGER.info("IM用户登录: " + imUserLoginNode.toString());
		// }

		/**
		 * 注册IM用户[批量生成用户然后注册]
		 */
		Long perNumber = 20l;
		String[] users = { "0mqtulzu", "44t5fk5o", "7mmwlyqf", "ctkmhnmg",
				"e3pfnydo", "eq7meytq", "fqhalzqs", "fr2jxllw", "fvcup4zb",
				"j1i0bdkm", "k1h8pj2f", "kbzxscrg", "kyhhbrjt", "ni67ujgh",
				"qfrknkm5", "qzsxpwde", "uquvt29s", "vqtyjkic", "wdmalq42",
				"wjhexwlv" };
		ObjectNode createNewIMUserBatchGenNode = createNewIMUserBatchGen(users,
				perNumber);
		// ArrayNode genericArrayNode = genericArrayNode(users);
		// ObjectNode objectNode = createNewIMUserBatch(genericArrayNode);
		if (null != createNewIMUserBatchGenNode) {
			logger
					.info("注册IM用户[批量]: "
							+ createNewIMUserBatchGenNode.toString());
		}

	}

	/**
	 * 注册环信用户
	 * 
	 * @param userName
	 *            用户名
	 * @param password
	 *            密码
	 * @return
	 */
	public static ObjectNode registerIMUser(String userName, String password) {
		ObjectNode datanode = JsonNodeFactory.instance.objectNode();
		datanode.put("username", userName);
		datanode.put("password", password);
		ObjectNode createNewIMUserSingleNode = null;
		int index = -1;
		while (true) {
			index++;
			createNewIMUserSingleNode = createNewIMUserSingle(datanode);
			if (null != createNewIMUserSingleNode
					&& null == createNewIMUserSingleNode.get("message")) {
				int stautsCode = createNewIMUserSingleNode.get("statusCode")
						.asInt();
				logger.info("用户名：" + userName + ",注册IM用户[单个]: 第" + index + "次"
						+ createNewIMUserSingleNode.toString());
				if (stautsCode == 200 || index == 5) {
					break;
				}
			} else {
				logger.info("用户名：" + userName + ",注册IM用户[单个]: 第" + index + "次"
						+ ",结果为空");
				if (index == 5) {
					break;
				} else {
					continue;
				}
			}
		}

		return createNewIMUserSingleNode;
	}

	/**
	 * 注册IM用户[单个]
	 * 
	 * 给指定AppKey创建一个新的用户
	 * 
	 * @param dataNode
	 * @return
	 */
	public static ObjectNode createNewIMUserSingle(ObjectNode dataNode) {
		ObjectNode objectNode = factory.objectNode();
		// check appKey format
		if (!JerseyUtils.match("^(?!-)[0-9a-zA-Z\\-]+#[0-9a-zA-Z]+", APPKEY)) {
			logger.error("Bad format of Appkey: " + APPKEY);

			objectNode.put("message", "Bad format of Appkey");

			return objectNode;
		}
		objectNode.removeAll();
		// check properties that must be provided
		if (null != dataNode && !dataNode.has("username")) {
			logger.error("Property that named username must be provided .");
			objectNode.put("message",
					"Property that named username must be provided .");
			return objectNode;
		}
		if (null != dataNode && !dataNode.has("password")) {
			logger.error("Property that named password must be provided .");

			objectNode.put("message",
					"Property that named password must be provided .");
			return objectNode;
		}
		try {
			JerseyWebTarget webTarget = EndPoints.USERS_TARGET.resolveTemplate(
					"org_name", APPKEY.split("#")[0]).resolveTemplate(
					"app_name", APPKEY.split("#")[1]);

			objectNode = JerseyUtils.sendRequest(webTarget, dataNode,
					credential, HTTPMethod.METHOD_POST, null);
		} catch (Exception e) {
			logger.error("注册IM用户时出现异常", e);
		}

		return objectNode;
	}

	/**
	 * 注册IM用户[批量]
	 * 
	 * 给指定AppKey创建一批用户
	 * 
	 * @param dataArrayNode
	 * @return
	 */
	public static ObjectNode createNewIMUserBatch(ArrayNode dataArrayNode) {
		ObjectNode objectNode = factory.objectNode();

		// check appKey format
		if (!JerseyUtils.match("^(?!-)[0-9a-zA-Z\\-]+#[0-9a-zA-Z]+", APPKEY)) {
			logger.error("Bad format of Appkey: " + APPKEY);
			objectNode.put("message", "Bad format of Appkey");
			return objectNode;
		}

		// check properties that must be provided
		if (dataArrayNode.isArray()) {
			for (JsonNode jsonNode : dataArrayNode) {
				if (null != jsonNode && !jsonNode.has("username")) {
					logger
							.error("Property that named username must be provided .");

					objectNode.put("message",
							"Property that named username must be provided .");

					return objectNode;
				}
				if (null != jsonNode && !jsonNode.has("password")) {
					logger
							.error("Property that named password must be provided .");

					objectNode.put("message",
							"Property that named password must be provided .");

					return objectNode;
				}
			}
		}

		try {

			JerseyWebTarget webTarget = EndPoints.USERS_TARGET.resolveTemplate(
					"org_name", APPKEY.split("#")[0]).resolveTemplate(
					"app_name", APPKEY.split("#")[1]);

			objectNode = JerseyUtils.sendRequest(webTarget, dataArrayNode,
					credential, HTTPMethod.METHOD_POST, null);

		} catch (Exception e) {
			logger.error("批量注册IM用户时出现异常", e);
		}

		return objectNode;
	}

	/**
	 * 注册IM用户[批量生成用户然后注册]
	 * 
	 * 给指定AppKey创建一批用户
	 * 
	 * @param perNumber
	 *            批量注册时一次注册的数量
	 * @return
	 */
	public static ObjectNode createNewIMUserBatchGen(String[] users,
			Long perNumber) {
		ObjectNode objectNode = factory.objectNode();

		if (users.length == 0 || perNumber == 0) {
			return objectNode;
		}

		System.out.println("你即将注册" + users.length + "个用户，如果大于" + perNumber
				+ "了,会分批注册,每次注册" + perNumber + "个");

		ArrayNode genericArrayNode = genericArrayNode(users);
		if (users.length <= perNumber) {
			objectNode = createNewIMUserBatch(genericArrayNode);
		} else {
			for (int i = 0; i < genericArrayNode.size(); i++) {
				ArrayNode tmpArrayNode = factory.arrayNode();
				tmpArrayNode.add(genericArrayNode.get(i));
				// 300 records on one migration
				if ((i + 1) % perNumber == 0) {
					objectNode = EasemobIMUsers
							.createNewIMUserBatch(genericArrayNode);
					tmpArrayNode.removeAll();
					continue;
				}

				// the rest records that less than the times of 300
				if (i > (genericArrayNode.size() / perNumber * perNumber - 1)) {
					objectNode = EasemobIMUsers
							.createNewIMUserBatch(genericArrayNode);
					tmpArrayNode.removeAll();
				}
			}
		}

		return objectNode;
	}

	/**
	 * 获取IM用户
	 * 
	 * @param userName
	 *            用户主键：username或者uuid
	 * @return
	 */
	public static ObjectNode getIMUsersByUserName(String userName) {
		ObjectNode objectNode = factory.objectNode();

		// check appKey format
		if (!JerseyUtils.match("^(?!-)[0-9a-zA-Z\\-]+#[0-9a-zA-Z]+", APPKEY)) {
			logger.error("Bad format of Appkey: " + APPKEY);

			objectNode.put("message", "Bad format of Appkey");

			return objectNode;
		}

		// check properties that must be provided
		if (StringUtils.isEmpty(userName)) {
			logger
					.error("The primaryKey that will be useed to query must be provided .");

			objectNode
					.put("message",
							"The primaryKey that will be useed to query must be provided .");

			return objectNode;
		}

		try {

			JerseyWebTarget webTarget = EndPoints.USERS_TARGET.resolveTemplate(
					"org_name", APPKEY.split("#")[0]).resolveTemplate(
					"app_name", APPKEY.split("#")[1]).path(userName);

			objectNode = JerseyUtils.sendRequest(webTarget, null, credential,
					HTTPMethod.METHOD_GET, null);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return objectNode;
	}

	/**
	 * 删除IM用户[单个]
	 * 
	 * 删除指定AppKey下IM单个用户
	 * 
	 * 
	 * @param userName
	 * @return
	 */
	public static ObjectNode deleteIMUserByUserName(String userName) {
		ObjectNode objectNode = factory.objectNode();

		// check appKey format
		if (!JerseyUtils.match("^(?!-)[0-9a-zA-Z\\-]+#[0-9a-zA-Z]+", APPKEY)) {
			logger.error("Bad format of Appkey: " + APPKEY);

			objectNode.put("message", "Bad format of Appkey");

			return objectNode;
		}

		try {
			JerseyWebTarget webTarget = EndPoints.USERS_TARGET.resolveTemplate(
					"org_name", APPKEY.split("#")[0]).resolveTemplate(
					"app_name", APPKEY.split("#")[1]).path(userName);

			objectNode = JerseyUtils.sendRequest(webTarget, null, credential,
					HTTPMethod.METHOD_DELETE, null);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return objectNode;
	}

	/**
	 * 删除IM用户[批量]
	 * 
	 * 批量指定AppKey下删除IM用户
	 * 
	 * @param limit
	 * @param queryStr
	 * @return
	 */
	public static ObjectNode deleteIMUserByUsernameBatch(Long limit,
			String queryStr) {

		ObjectNode objectNode = factory.objectNode();

		// check appKey format
		if (!JerseyUtils.match("^(?!-)[0-9a-zA-Z\\-]+#[0-9a-zA-Z]+", APPKEY)) {
			logger.error("Bad format of Appkey: " + APPKEY);

			objectNode.put("message", "Bad format of Appkey");

			return objectNode;
		}

		try {

			JerseyWebTarget webTarget = EndPoints.USERS_TARGET.resolveTemplate(
					"org_name", APPKEY.split("#")[0]).resolveTemplate(
					"app_name", APPKEY.split("#")[1])
					.queryParam("ql", queryStr).queryParam("limit",
							String.valueOf(limit));

			objectNode = JerseyUtils.sendRequest(webTarget, null, credential,
					HTTPMethod.METHOD_DELETE, null);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return objectNode;
	}

	/**
	 * 重置IM用户密码 提供管理员token
	 * 
	 * @param userName
	 * @param dataObjectNode
	 * @return
	 */
	public static ObjectNode modifyIMUserPasswordWithAdminToken(
			String userName, ObjectNode dataObjectNode) {
		ObjectNode objectNode = factory.objectNode();

		// check appKey format
		if (!JerseyUtils.match("^(?!-)[0-9a-zA-Z\\-]+#[0-9a-zA-Z]+", APPKEY)) {
			logger.error("Bad format of Appkey: " + APPKEY);

			objectNode.put("message", "Bad format of Appkey");

			return objectNode;
		}

		if (StringUtils.isEmpty(userName)) {
			logger
					.error("Property that named userName must be provided，the value is username or uuid of imuser.");

			objectNode
					.put(
							"message",
							"Property that named userName must be provided，the value is username or uuid of imuser.");

			return objectNode;
		}

		if (null != dataObjectNode && !dataObjectNode.has("newpassword")) {
			logger.error("Property that named newpassword must be provided .");

			objectNode.put("message",
					"Property that named newpassword must be provided .");

			return objectNode;
		}

		try {

			JerseyWebTarget webTarget = EndPoints.USERS_TARGET.resolveTemplate(
					"org_name", APPKEY.split("#")[0]).resolveTemplate(
					"app_name", APPKEY.split("#")[1]).path(userName).path(
					"password");

			objectNode = JerseyUtils.sendRequest(webTarget, dataObjectNode,
					credential, HTTPMethod.METHOD_PUT, null);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return objectNode;
	}

	/**
	 * 添加好友[单个]
	 * 
	 * @param ownerUserName
	 * @param friendUserName
	 * 
	 * @return
	 */
	public static ObjectNode addFriendSingle(String ownerUserName,
			String friendUserName) {
		ObjectNode objectNode = factory.objectNode();

		// check appKey format
		if (!JerseyUtils.match("^(?!-)[0-9a-zA-Z\\-]+#[0-9a-zA-Z]+", APPKEY)) {
			logger.error("Bad format of Appkey: " + APPKEY);

			objectNode.put("message", "Bad format of Appkey");

			return objectNode;
		}

		if (StringUtils.isEmpty(ownerUserName)) {
			logger
					.error("Your userName must be provided，the value is username or uuid of imuser.");

			objectNode
					.put("message",
							"Your userName must be provided，the value is username or uuid of imuser.");

			return objectNode;
		}

		if (StringUtils.isEmpty(friendUserName)) {
			logger
					.error("The userName of friend must be provided，the value is username or uuid of imuser.");

			objectNode
					.put(
							"message",
							"The userName of friend must be provided，the value is username or uuid of imuser.");

			return objectNode;
		}

		try {
			JerseyWebTarget webTarget = EndPoints.USERS_ADDFRIENDS_TARGET
					.resolveTemplate("org_name", APPKEY.split("#")[0])
					.resolveTemplate("app_name", APPKEY.split("#")[1])
					.resolveTemplate("ownerUserName", ownerUserName)
					.resolveTemplate("friendUserName", friendUserName);

			ObjectNode body = factory.objectNode();
			objectNode = JerseyUtils.sendRequest(webTarget, body, credential,
					HTTPMethod.METHOD_POST, null);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return objectNode;
	}

	/**
	 * 解除好友关系
	 * 
	 * @param ownerUserName
	 * @param friendUserName
	 * 
	 * @return
	 */
	public static ObjectNode deleteFriendSingle(String ownerUserName,
			String friendUserName) {
		ObjectNode objectNode = factory.objectNode();

		// check appKey format
		if (!JerseyUtils.match("^(?!-)[0-9a-zA-Z\\-]+#[0-9a-zA-Z]+", APPKEY)) {
			logger.error("Bad format of Appkey: " + APPKEY);

			objectNode.put("message", "Bad format of Appkey");

			return objectNode;
		}

		if (StringUtils.isEmpty(ownerUserName)) {
			logger
					.error("Your userName must be provided，the value is username or uuid of imuser.");

			objectNode
					.put("message",
							"Your userName must be provided，the value is username or uuid of imuser.");

			return objectNode;
		}

		if (StringUtils.isEmpty(friendUserName)) {
			logger
					.error("The userName of friend must be provided，the value is username or uuid of imuser.");

			objectNode
					.put(
							"message",
							"The userName of friend must be provided，the value is username or uuid of imuser.");

			return objectNode;
		}

		try {

			JerseyWebTarget webTarget = EndPoints.USERS_ADDFRIENDS_TARGET
					.resolveTemplate("org_name", APPKEY.split("#")[0])
					.resolveTemplate("app_name", APPKEY.split("#")[1])
					.resolveTemplate("ownerUserName", ownerUserName)
					.resolveTemplate("friendUserName", friendUserName);

			ObjectNode body = factory.objectNode();
			objectNode = JerseyUtils.sendRequest(webTarget, body, credential,
					HTTPMethod.METHOD_DELETE, null);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return objectNode;
	}

	/**
	 * 查看好友
	 * 
	 * @param ownerUserName
	 * 
	 * @return
	 */
	public static ObjectNode getFriends(String ownerUserName) {
		ObjectNode objectNode = factory.objectNode();

		// check appKey format
		if (!JerseyUtils.match("^(?!-)[0-9a-zA-Z\\-]+#[0-9a-zA-Z]+", APPKEY)) {
			logger.error("Bad format of Appkey: " + APPKEY);

			objectNode.put("message", "Bad format of Appkey");

			return objectNode;
		}

		if (StringUtils.isEmpty(ownerUserName)) {
			logger
					.error("Your userName must be provided，the value is username or uuid of imuser.");

			objectNode
					.put("message",
							"Your userName must be provided，the value is username or uuid of imuser.");

			return objectNode;
		}

		try {
			JerseyWebTarget webTarget = EndPoints.USERS_ADDFRIENDS_TARGET
					.resolveTemplate("org_name", APPKEY.split("#")[0])
					.resolveTemplate("app_name", APPKEY.split("#")[1])
					.resolveTemplate("ownerUserName", ownerUserName)
					.resolveTemplate("friendUserName", "");

			ObjectNode body = factory.objectNode();
			objectNode = JerseyUtils.sendRequest(webTarget, body, credential,
					HTTPMethod.METHOD_GET, null);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return objectNode;
	}

	/**
	 * IM用户登录
	 * 
	 * @param ownerUserName
	 * @param password
	 * 
	 * @return
	 */
	public static ObjectNode imUserLogin(String ownerUserName, String password) {

		ObjectNode objectNode = factory.objectNode();

		// check appKey format
		if (!JerseyUtils.match("^(?!-)[0-9a-zA-Z\\-]+#[0-9a-zA-Z]+", APPKEY)) {
			logger.error("Bad format of Appkey: " + APPKEY);

			objectNode.put("message", "Bad format of Appkey");

			return objectNode;
		}
		if (StringUtils.isEmpty(ownerUserName)) {
			logger
					.error("Your userName must be provided，the value is username or uuid of imuser.");

			objectNode
					.put("message",
							"Your userName must be provided，the value is username or uuid of imuser.");

			return objectNode;
		}
		if (StringUtils.isEmpty(password)) {
			logger
					.error("Your password must be provided，the value is username or uuid of imuser.");

			objectNode
					.put("message",
							"Your password must be provided，the value is username or uuid of imuser.");

			return objectNode;
		}

		try {
			ObjectNode dataNode = factory.objectNode();
			dataNode.put("grant_type", "password");
			dataNode.put("username", ownerUserName);
			dataNode.put("password", password);

			List<NameValuePair> headers = new ArrayList<NameValuePair>();
			headers.add(new BasicNameValuePair("Content-Type",
					"application/json"));

			objectNode = JerseyUtils.sendRequest(EndPoints.TOKEN_APP_TARGET
					.resolveTemplate("org_name", APPKEY.split("#")[0])
					.resolveTemplate("app_name", APPKEY.split("#")[1]),
					dataNode, null, HTTPMethod.METHOD_POST, headers);

		} catch (Exception e) {
			throw new RuntimeException(
					"Some errors ocuured while fetching a token by usename and passowrd .");
		}

		return objectNode;
	}

	/**
	 * 添加黑名单
	 * 
	 * @param ownerUserName
	 * @param dataArrayNode
	 * 
	 * @return
	 */
	public static ObjectNode addBlacklist(String ownerUserName,
			String[] userNames) {
		ObjectNode objectNode = factory.objectNode();

		// check appKey format
		if (!JerseyUtils.match("^(?!-)[0-9a-zA-Z\\-]+#[0-9a-zA-Z]+", APPKEY)) {
			logger.error("Bad format of Appkey: " + APPKEY);
			objectNode.put("message", "Bad format of Appkey");
			return objectNode;
		}

		if (StringUtils.isEmpty(ownerUserName)) {
			logger
					.error("Your userName must be provided，the value is username or uuid of imuser.");

			objectNode
					.put("message",
							"Your userName must be provided，the value is username or uuid of imuser.");
			return objectNode;
		}
		try {
			JerseyWebTarget webTarget = EndPoints.USERS_ADDBLACKLIST_TARGET
					.resolveTemplate("org_name", APPKEY.split("#")[0])
					.resolveTemplate("app_name", APPKEY.split("#")[1])
					.resolveTemplate("ownerUserName", ownerUserName);
			ArrayNode arrayNode = factory.arrayNode();
			for (String userName : userNames) {
				arrayNode.add(userName);
			}
			ObjectNode userNode = factory.objectNode();
			userNode.put("usernames", arrayNode);

			objectNode = JerseyUtils.sendRequest(webTarget, userNode,
					credential, HTTPMethod.METHOD_POST, null);
			if (objectNode != null) {
				logger.info("IM用户加入黑名单: " + objectNode.toString());
			}

		} catch (Exception e) {
			logger.error("用户添加黑名单时出现异常", e);
		}

		return objectNode;
	}

	/**
	 * 解除黑名单
	 * 
	 * @param ownerUserName
	 * @param friendUserName
	 * 
	 * @return
	 */
	public static ObjectNode deleteBlacklist(String ownerUserName,
			String blockedUserName) {
		ObjectNode objectNode = factory.objectNode();

		// check appKey format
		if (!JerseyUtils.match("^(?!-)[0-9a-zA-Z\\-]+#[0-9a-zA-Z]+", APPKEY)) {
			logger.error("Bad format of Appkey: " + APPKEY);
			objectNode.put("message", "Bad format of Appkey");
			return objectNode;
		}

		if (StringUtils.isEmpty(ownerUserName)) {
			logger
					.error("Your userName must be provided，the value is username or uuid of imuser.");

			objectNode
					.put("message",
							"Your userName must be provided，the value is username or uuid of imuser.");

			return objectNode;
		}

		if (StringUtils.isEmpty(blockedUserName)) {
			logger
					.error("The userName of blocked must be provided，the value is username or uuid of imuser.");

			objectNode
					.put(
							"message",
							"The userName of blocked must be provided，the value is username or uuid of imuser.");

			return objectNode;
		}
		try {
			JerseyWebTarget webTarget = EndPoints.USERS_DELETEBLACKLIST_TARGET
					.resolveTemplate("org_name", APPKEY.split("#")[0])
					.resolveTemplate("app_name", APPKEY.split("#")[1])
					.resolveTemplate("ownerUserName", ownerUserName)
					.resolveTemplate("blockedUserame", blockedUserName);

			ObjectNode body = factory.objectNode();
			objectNode = JerseyUtils.sendRequest(webTarget, body, credential,
					HTTPMethod.METHOD_DELETE, null);
			if (objectNode != null) {
				logger.info("IM用户解除黑名单: " + objectNode.toString());
			}

		} catch (Exception e) {
			logger.error("用户解除黑名单时出现异常", e);
		}

		return objectNode;
	}

	/**
	 * 指定前缀和数量生成用户基本数据
	 * 
	 * @param usernamePrefix
	 * @param number
	 * @return
	 */
	private static ArrayNode genericArrayNode(String[] users) {
		ArrayNode arrayNode = factory.arrayNode();
		// String[] users = { "dflob6yi", "wg9uotow", "ul2cuzml", "ku1qcfl2",
		// "5vxrju0r", "fg1crqfg", "1rpbp6dz", "c9qfrnbj", "zp3fyapc",
		// "fihd1x5c", "uq9oicti", "b4nvw52m", "zxvdukxl", "mtdinoj3",
		// "tb1jdrle", "cxxfayrq", "kecf8nej", "knyhmfn7", "y6xjbnib",
		// "enxp3s3v", "ghkra7bm", "xqxgellt", "2jabsrhe", "cywmps9t",
		// "kmnzqpf4" };
		for (int i = 0; i < users.length; i++) {
			ObjectNode userNode = factory.objectNode();
			userNode.put("username", users[i]);
			userNode.put("password", users[i]);
			arrayNode.add(userNode);
		}

		return arrayNode;
	}

}
